//区域覆盖分析的JAVA类
package com.ejoysoft.wlanyz.business;

import com.ejoysoft.auth.MD5;
import com.ejoysoft.common.*;
import com.ejoysoft.common.exception.UserUnitIdException;
import com.ejoysoft.wlanyz.system.Unit;
import com.ejoysoft.wlanyz.system.User;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import org.jfree.ui.Size2D;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.HashMap;
import java.util.Date;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;

public class Regional{
	private Globa globa;
    private DbConnect db;


    public Regional() {
    }

    public Regional(Globa globa) {
        this.globa = globa;
        db = globa.db;
    }

    public Regional(Globa globa, boolean b) {
        this.globa = globa;
        db = globa.db;
        if (b) globa.setDynamicProperty(this);
    }
    String strTableName="wlan_task_info";
    String strTableName1="WLAN_TEST_RESULT";
    String strTableName2="WLAN_RESC_HOTPOT";
    String strTableName3="RM_DEVICE";
    //记录集转化为对象
    public Regional load (ResultSet rs,boolean isView){
    	Regional smLoad = new Regional();
		try {
			smLoad.setStrLongitude(rs.getString("STRLONGITUDE"));
			smLoad.setStrLatitude(rs.getString("STRLATITUDE"));
			smLoad.setStrHotpotname(rs.getString("STRHOTPOTNAME"));
			smLoad.setStrCity(rs.getString("STRCITY"));
			smLoad.setStrTown(rs.getString("STRTOWN"));
			smLoad.setStrHotpotType(rs.getString("STRHOTPOTTYPE"));
			smLoad.setStrHotpotAddr(rs.getString("STRHOTPOTADDR"));
			smLoad.setStrArea(rs.getString("STRAREA"));
			smLoad.setStrTechonlog(rs.getString("STRTECHONLOG"));
			smLoad.setStrhpid(rs.getString("STRID"));
		} catch (Exception e){
			e.printStackTrace();
		}
		return smLoad;
	}
  
    private String strhpid;
    

    public String getStrhpid() {
		return strhpid;
	}

	public void setStrhpid(String strhpid) {
		this.strhpid = strhpid;
	}

	//列表记录集
    public Vector<Regional> list(String where,int startRow, int rowCount) {
        Vector<Regional> beans = new Vector<Regional>();
        String where2 = " where 1=1 ";
        where="";
        List<String> list = returnhpid(where);  //获取所有与已测试任务相关的热点名称
 	   	where2 += "and ( 1=2"; 
    	   
        for (int i = 0; i < list.size(); i++) {
			where2 += " or STRid ='"+list.get(i)+"'"; 
        }
    		 where2 +=")";
      
        try {
      	  String sql= "select *  from "+ strTableName2 + where2;
            Statement s = db.getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            if (startRow != 0 && rowCount != 0)
                s.setMaxRows((startRow + rowCount) - 1);
            ResultSet rs = s.executeQuery(sql);
            if (rs != null && rs.next()) {
                if (startRow != 0 && rowCount != 0)
                    rs.absolute(startRow);
                do {
                	Regional theBean = new Regional();
                    theBean = load(rs, false);
                    beans.addElement(theBean);
                } while (rs.next());
            }
            rs.close();
            s.close();
        } catch (Exception ee) {
            ee.printStackTrace();
        }
        db.closeCon();
        return beans;
    }
    //获取每个热点已测AP
    public List<String> returntestAP(String where) {	  
    	String where2 = " where 1=1 ";
        List<String> list = returnAPmac();  //获取测试结果表中的所有任务ID
 	   	where2 += "and ( 1=2"; 
    	   
        for (int i = 0; i < list.size(); i++) {
			where2 += " or STRMAC_ADDR ='"+list.get(i)+"'"; 
        }
    	where2 +=")";
    	List<String> list2 = new ArrayList<String>();
        try {
            String strSql = "select * from  rm_device " + where2 + where;
            ResultSet rs = db.executeQuery(strSql);
            if (rs != null){
          	 while (rs.next()) {
                   list2.add(rs.getString("device_name"));
				}
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        db.closeCon();
        return list2; //返回所有相关的热点名称
    }
    //获取所有已测AP的MAC
    public List<String> returnAPmac() {
  	  List<String> list = new ArrayList<String>();
        try {
            String strSql = "select distinct strapmac from  " + strTableName1;
            ResultSet rs = db.executeQuery(strSql);
            if (rs != null){
          	 while (rs.next()) {
                   list.add(rs.getString("strapmac"));
				}
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        db.closeCon();
        return list;
    }
    //获取热点ID
    public List<String> returnhpid(String where) {	  
    	String where2 = " where 1=1 ";
        where="";
        List<String> list = returnTskId(where);  //获取测试结果表中的所有任务ID
 	   	where2 += "and ( 1=2"; 
    	   
        for (int i = 0; i < list.size(); i++) {
			where2 += " or STRTASKID ='"+list.get(i)+"'"; 
        }
    		 where2 +=")";
    	
        try {
            String strSql = "select * from  " + strTableName + where2;
            ResultSet rs = db.executeQuery(strSql);
            if (rs != null){
          	 while (rs.next()) {
                   list.add(rs.getString("STRHOTPOTNAME"));
				}
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        db.closeCon();
        return list; //返回所有相关的热点名称
    }
    //获取任务ID
    public List<String> returnTskId(String where) {
    	  List<String> list = new ArrayList<String>();
          try {
              String strSql = "select * from  " + strTableName1 + " ".concat(where);
              ResultSet rs = db.executeQuery(strSql);
              if (rs != null){
            	 while (rs.next()) {
                     list.add(rs.getString("STRTASKID"));
  				}
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
          db.closeCon();
          return list;
      }
    
  //获取热点下AP信息
    public List<String> returnAP(String where) {
    	  List<String> list = new ArrayList<String>();
          try {
              String strSql = "select * from  " + strTableName3 + " ".concat(where);
              ResultSet rs = db.executeQuery(strSql);
              if (rs != null){
            	 while (rs.next()) {
                     list.add(rs.getString("DEVICE_NAME"));
  				}
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
          db.closeCon();
          return list;
      }
    
    //列表记录集
    public int getCount(String where) {
        int count = 0;

        try {
      	  String sql = "select count(strId) from "+strTableName;
           
            ResultSet rs = db.executeQuery(sql);
            if (rs.next())
                count = rs.getInt(1);   
            rs.close();
            db.closeCon();
            return count;
           
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e); 
            db.closeCon();
            return count;
        }
    }
    public Regional show(String where) {
        try {
            String strSql = "select * from  " + strTableName;
            ResultSet rs = db.executeQuery(strSql);
            if (rs != null && rs.next())
            	{db.closeCon();
                return load(rs, true);}
            else{
            	db.closeCon();
                return null;
            }
        } catch (Exception e) {
        	db.closeCon();
        	return null;
        }
    }

   private String strId;//	id号
   private String strTaskid;//任务号
   private String strhotpotname;//热点名
   private String strLongitude;
   private String strLatitude;
   private String strHotpotname;
   private String strCity;
   private String strTown;
   private String strHotpotType;
   private String strHotpotAddr;
   private String strArea;
   private String strTechonlog;

   public String getStrTechonlog() {
		return strTechonlog;
	}

	public void setStrTechonlog(String strTechonlog) {
		this.strTechonlog = strTechonlog;
	}
   
   public String getStrArea() {
		return strArea;
	}

	public void setStrArea(String strArea) {
		this.strArea = strArea;
	}
   
   public String getStrHotpotAddr() {
		return strHotpotAddr;
	}

	public void setStrHotpotAddr(String strHotpotAddr) {
		this.strHotpotAddr = strHotpotAddr;
	}
   
   public String getStrHotpotType() {
		return strHotpotType;
	}

	public void setStrHotpotType(String strHotpotType) {
		this.strHotpotType = strHotpotType;
	}
   
   public String getStrTown() {
		return strTown;
	}

	public void setStrTown(String strTown) {
		this.strTown = strTown;
	}
   
   public String getStrCity() {
		return strCity;
	}

	public void setStrCity(String strCity) {
		this.strCity = strCity;
	}
   
   public String getStrHotpotname() {
		return strHotpotname;
	}

	public void setStrHotpotname(String strHotpotname) {
		this.strHotpotname = strHotpotname;
	}
   
	public String getStrId() {
		return strId;
	}
	
	public String getstrhotpotname() {
		return strhotpotname;
	}

	public void setStrId(String strId) {
		this.strId = strId;
	}

	public String getStrTaskid() {
		return strTaskid;
	}

	public void setStrTaskid(String strTaskid) {
		this.strTaskid = strTaskid;
	}
	
	public String getStrLongitude() {
		return strLongitude;
	}

	public void setStrLongitude(String strLongitude) {
		this.strLongitude = strLongitude;
	}
	
	public String getStrLatitude() {
		return strLatitude;
	}

	public void setStrLatitude(String strLatitude) {
		this.strLatitude = strLatitude;
	}
	
	

}